package com.exm.collectcodenew.juc.aqs;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
//Semaphore的复用功能
public class semaphoreTest2 {
        //创建一个Semaphore实例
        private static Semaphore semaphore= new Semaphore(0);

        public static void main(String[] args) throws InterruptedException {
            ExecutorService executorService = Executors.newFixedThreadPool(2);
            //将线程A添加到线程池
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread()+"A task over");
                    semaphore.release();
                }
            });
            //将线程B添加到线程池
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread()+"A task over");
                    semaphore.release();
                }
            });
            //(1)等待子线程执行完毕A完毕，返回
            semaphore.acquire(2);
            //将线程C添加到线程池
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread()+"B task over");
                    semaphore.release();
                }
            });
            //将线程D添加到线程池
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread()+"B task over");
                    semaphore.release();
                }
            });
            //(2)等待子线程执行完毕B完毕，返回
            semaphore.acquire(2);

            System.out.println("task is over!");
            //关闭线程池
            executorService.shutdown();
        }
}
